//
//  FYFirstListCell.m
//  ThePainter
//
//  Created by Sean on 15/8/13.
//  Copyright (c) 2015年 FeiYu. All rights reserved.
//

#import "FYFirstListCell.h"
#import "FYFirstListModel.h"


@interface FYFirstListCell ()

@property (nonatomic) FYFirstListModel *model;
@property (nonatomic) CGFloat w;
@property (nonatomic) CGFloat h;
@property (nonatomic) UIImageView *img;
@property (nonatomic) UIButton *avatar;
@property (nonatomic) FYLabel *labNickname;
@property (nonatomic) FYLabel *labLikeCount;
@property (nonatomic) FYLabel *labComCount;
@property (nonatomic) UILabel *labDesc;

- (void)initView;

@end

@implementation FYFirstListCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        self.w = [FYUtils fyScreenWidth];
        self.h = [FYUtils fyScreenWidth] + [FYUtils fyScreenWidth] / 3;
        [self initView];
    }
    return self;
}

- (void)initView {
    
//    self.frame = CGRectMake(0, 0, self.w, self.h);
    self.selectionStyle = UITableViewCellSelectionStyleNone;
//    self.backgroundColor = [UIColor clearColor];
    self.backgroundColor = fyColorLightGary;
    
    //背景
    CGFloat conW = self.w;
    CGFloat conH = self.h / 100 * 99;
    CGFloat conX = 0;
    CGFloat conY = self.h / 100 * 1;
    UIView *container = [[UIView alloc] initWithFrame:CGRectMake(conX, conY, conW, conH)];
    container.backgroundColor = [UIColor whiteColor];
    [self.contentView addSubview:container];
    
    //图片
    CGFloat imgW = conW;
    CGFloat imgH = imgW;
    CGFloat imgX = 0;
    CGFloat imgY = conH / 100 * 8;
    self.img = [[UIImageView alloc] initWithFrame:CGRectMake(imgX, imgY, imgW, imgH)];
    self.img.backgroundColor = [UIColor clearColor];
    [container addSubview:self.img];
    
    //头像
    CGFloat acW = conW / 100 * 13;
    CGFloat acH = acW;
    CGFloat acX = conW / 100 * 3;
    CGFloat acY = conH / 100 * 2;
    UIView *aCon = [[UIView alloc] initWithFrame:CGRectMake(acX, acY, acW, acH)];
    aCon.backgroundColor = [UIColor whiteColor];
    aCon.layer.cornerRadius = acW / 2;
    [container addSubview:aCon];
    CGFloat aW = acW / 100 * 90;
    CGFloat aH = aW;
    CGFloat aX = acW / 2;
    CGFloat aY = acH / 2;
    self.avatar = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, aW, aH)];
    self.avatar.center = CGPointMake(aX, aY);
    self.avatar.backgroundColor = [UIColor clearColor];
    self.avatar.layer.masksToBounds = YES;
    self.avatar.layer.cornerRadius = aW / 2;
    [self.avatar addTarget:self action:@selector(avatarAction) forControlEvents:UIControlEventTouchUpInside];
    [aCon addSubview:self.avatar];
    
    //昵称
    CGFloat labNickX = acX + acW + acX;
    CGFloat labNickY = acY + acH / 5;
    self.labNickname = [[FYLabel alloc] initWithString:@"" size:fyLabSize3 color:[UIColor blackColor]];
    self.labNickname.frame = fyCGRectSetOrigin(labNickX, labNickY, self.labNickname.frame);
    [container addSubview:self.labNickname];
    
    //横线
    CGFloat l1W = conW;
    CGFloat l1H = 0.5;
    CGFloat l1X = 0;
    CGFloat l1Y = conH / 100 * 93;
    UIView *line1 = [[UIView alloc] initWithFrame:CGRectMake(l1X, l1Y, l1W, l1H)];
    line1.backgroundColor = fyColorLightGary;
    [container addSubview:line1];
    
    //竖线
    CGFloat l2W = 0.5;
    CGFloat l2H = conH / 100 * 5;
    CGFloat l2X = conW / 3 * 1;
    CGFloat l2Y = conH / 100 * 94;
    UIView *line2 = [[UIView alloc] initWithFrame:CGRectMake(l2X, l2Y, l2W, l2H)];
    line2.backgroundColor = fyColorLightGary;
    [container addSubview:line2];
    
    //竖线
    CGFloat l3W = 0.5;
    CGFloat l3H = conH / 100 * 5;
    CGFloat l3X = conW / 3 * 2;
    CGFloat l3Y = conH / 100 * 94;
    UIView *line3 = [[UIView alloc] initWithFrame:CGRectMake(l3X, l3Y, l3W, l3H)];
    line3.backgroundColor = fyColorLightGary;
    [container addSubview:line3];
    
    //简介
    CGFloat labDescW = conW / 100 * 98;
    CGFloat labDescH = conH / 100 * 9;
    CGFloat labDescX = conW / 2 - labDescW / 2;
    CGFloat labDescY = conH / 100 * 84;
    self.labDesc = [[UILabel alloc] initWithFrame:CGRectMake(labDescX, labDescY, labDescW, labDescH)];
    self.labDesc.backgroundColor = [UIColor clearColor];
    self.labDesc.text = @"";
    self.labDesc.textAlignment = NSTextAlignmentLeft;
    self.labDesc.font = [UIFont systemFontOfSize:fyLabSize3];
    //不做行数限制
    self.labDesc.numberOfLines = 0;
    //结尾为省略号
    self.labDesc.lineBreakMode = NSLineBreakByTruncatingTail;
    [container addSubview:self.labDesc];
    
    CGFloat btnW = conW / 3 * 1;
    CGFloat btnH = conH / 100 * 7;
    CGFloat imgIconW = btnH / 100 * 40;
    CGFloat imgIconH = imgIconW;
    CGFloat imgIconX = btnW / 100 * 25;
    CGFloat imgIconY = btnH / 2 - imgIconH / 2;
    CGFloat labTitleX = btnW / 2;
    CGFloat labTitleY = btnH / 2;
    CGFloat labCountX = btnW / 100 * 70;
    CGFloat labCountY = btnH / 2;
    
    //点赞
    CGFloat btnLikeX = 0;
    CGFloat btnLikeY = conH / 100 * 93;
    UIButton *btnLike = [[UIButton alloc] initWithFrame:CGRectMake(btnLikeX, btnLikeY, btnW, btnH)];
    btnLike.backgroundColor = [UIColor clearColor];
    [container addSubview:btnLike];
    
    UIImageView *imgLike = [[UIImageView alloc] initWithFrame:CGRectMake(imgIconX, imgIconY, imgIconW, imgIconH)];
    imgLike.image = [UIImage imageNamed:@"icon_praise"];
    [btnLike addSubview:imgLike];
    
    FYLabel *labLike = [[FYLabel alloc] initWithString:@"赞" size:fyLabSize2 color:fyColorLightGary];
    labLike.center = CGPointMake(labTitleX, labTitleY);
    [btnLike addSubview:labLike];
    
    self.labLikeCount = [[FYLabel alloc] initWithString:@"" size:fyLabSize2 color:fyColorLightGary];
    self.labLikeCount.center = CGPointMake(labCountX, labCountY);
    [btnLike addSubview:self.labLikeCount];
    
    //评论
    CGFloat btnComX = conW / 3 * 1;
    CGFloat btnComY = conH / 100 * 93;
    UIButton *btnCom = [[UIButton alloc] initWithFrame:CGRectMake(btnComX, btnComY, btnW, btnH)];
    btnCom.backgroundColor = [UIColor clearColor];
    [container addSubview:btnCom];
    
    UIImageView *imgCom = [[UIImageView alloc] initWithFrame:CGRectMake(imgIconX, imgIconY, imgIconW, imgIconH)];
    imgCom.image = [UIImage imageNamed:@"icon_comment"];
    [btnCom addSubview:imgCom];
    
    FYLabel *labCom = [[FYLabel alloc] initWithString:@"评论" size:fyLabSize2 color:fyColorLightGary];
    labCom.center = CGPointMake(labTitleX, labTitleY);
    [btnCom addSubview:labCom];
    
    self.labComCount = [[FYLabel alloc] initWithString:@"" size:fyLabSize2 color:fyColorLightGary];
    self.labComCount.center = CGPointMake(labCountX, labCountY);
    [btnCom addSubview:self.labComCount];
    
    //分享
    CGFloat btnShareX = conW / 3 * 2;
    CGFloat btnShareY = conH / 100 * 93;
    UIButton *btnShare = [[UIButton alloc] initWithFrame:CGRectMake(btnShareX, btnShareY, btnW, btnH)];
    btnShare.backgroundColor = [UIColor clearColor];
    [container addSubview:btnShare];
    
    UIImageView *imgShare = [[UIImageView alloc] initWithFrame:CGRectMake(imgIconX, imgIconY, imgIconW, imgIconH)];
    imgShare.image = [UIImage imageNamed:@"icon_share"];
    [btnShare addSubview:imgShare];
    
    FYLabel *labShare = [[FYLabel alloc] initWithString:@"分享" size:fyLabSize2 color:fyColorLightGary];
    labShare.center = CGPointMake(labTitleX, labTitleY);
    [btnShare addSubview:labShare];
    
}

- (void)loadModel:(id)model {
    
    self.model = model;
    
    //设置SDWebImageManager
    SDWebImageManager *manager = [SDWebImageManager sharedManager];
    //下载图片
    [manager downloadImageWithURL:[NSURL URLWithString:self.model.product.productPic] options:0 progress:^(NSInteger receivedSize, NSInteger expectedSize) {
        self.img.image = [UIImage imageNamed:@"loading_small"];
    } completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) {
         if (image) {
             //处理图片
             dispatch_async(dispatch_get_global_queue(0, 0), ^{
                 UIImage *newImage = [self dealWithImage:image];
                 //回到主线程，显示图片
                 dispatch_async(dispatch_get_main_queue(), ^{
                     self.img.image = newImage;
                     self.isLoadModel = YES;
                 });
             });
         }
    }];
    
    [manager downloadImageWithURL:[NSURL URLWithString:self.model.user.avatar] options:0 progress:^(NSInteger receivedSize, NSInteger expectedSize) {
        [self.avatar setImage:[UIImage imageNamed:@"icon_set_avatar"] forState:UIControlStateNormal];
    } completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) {
        [self.avatar setImage:image forState:UIControlStateNormal];
    }];
    
    [self.labNickname setStringAndLeft:self.model.user.username];
    [self.labDesc setText:self.model.product.desc];
    [self.labLikeCount setStringAndCenter:self.model.product.likeCount];
    [self.labComCount setStringAndCenter:self.model.product.commentCount];
}

- (UIImage*)dealWithImage:(UIImage*)image {
    CGFloat w = image.size.width;
    CGFloat h = image.size.height;
    
    CGFloat newW = 0;
    CGFloat newH = 0;
    CGFloat newX = 0;
    CGFloat newY = 0;
    
    if (w > h) {
        newW = h;
        newH = h;
        newX = newW / 2 - w / 2;
        newY = 0;
    } else {
        newW = w;
        newH = w;
        newX = 0;
        newY = newH / 2 - h / 2;
    }
    
    //剪裁图片
    UIGraphicsBeginImageContext(CGSizeMake(newW, newH));
    [image drawInRect:CGRectMake(newX, newY, w, h)];
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    
    return newImage;
}

- (void)avatarAction {
    [self.delegate didSelectAvatar:self.model.user.userId];
}

- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
